﻿using Furion.DatabaseAccessor;
using Furion.DataEncryption.Extensions;
using Furion.DependencyInjection;
using Furion.FriendlyException;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Mrhd.Core.Entities;
using Mrhd.Core.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Mrhd.Application
{
    public class UserService : IUserService, IScoped
    {
        private readonly IRepository<UserEntity> _repository;
        private readonly IRepository<UserMobileEntity> _wxRepository;
        public UserService(IRepository<UserEntity> repository,
            IRepository<UserMobileEntity> wxRepository)
        {
            _repository = repository;
            _wxRepository = wxRepository;
        }

        public async Task<UserEntity> CreateAsync(UserEntity user)
        {
            if (await _repository.AnyAsync(a => a.UserName.ToLower() == user.UserName))
            {
                throw Oops.Oh("用户名已存在！");
            }
            user.CreatedTime = DateTimeOffset.Now;
            user.IsActive = true;
            user.Password = user.Password.ToMD5Encrypt();
            await _repository.SaveNowAsync();
            return user;
        }
    }
}
